package com.lhy.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhy.domain.User;
import com.lhy.entity.PageResult;
import com.lhy.entity.Result;
import com.lhy.user.service.UserService;
import com.lhy.user.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户表
 */
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService iUserService;


    /**
     * 查询全部用户表
     *
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<User>> selectAll() {
        return Result.ok("查询成功", iUserService.selectAll());
    }


    /**
     * 条件查询用户表
     *
     * @param iUser
     * @return
     */
    @PostMapping("/selectList")
    public Result<List<User>> select(@RequestBody User iUser) {
        return Result.ok("条件查询成功", iUserService.selectList(iUser));
    }

    /**
     * 新增用户表
     *
     * @param iUser
     * @return
     */
    @PostMapping("/insert")
    public Result<Integer> insert(@RequestBody User iUser) {
        Integer i = iUserService.insert(iUser);
        if (i==0){
            return Result.fail("新增失败",i);
        } else if (i==-1) {
            return Result.fail("新增失败,用户名不可重复",i);
        }
        return Result.ok("新增成功",i);
    }


    /**
     * 删除用户表
     *
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result<Integer> delete(@RequestParam String ids) {
        Integer delete = iUserService.delete(ids);
        if (delete==0){
            return Result.fail("删除失败",delete);
        }
        return Result.ok("删除成功",delete);
    }


    /**
     * 修改用户表
     *
     * @param iUser
     * @return
     */
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody User iUser) {
        Integer update = iUserService.update(iUser);
        if (update==0){
            return Result.fail("修改失败",update);
        }
        return Result.ok("修改成功",update);
    }

    /**
     * 分页搜索实现用户表
     *
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    @GetMapping(value = "/pageAll")
    public Result<PageResult<User>> pageAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<User> pageList = iUserService.pageAll(page, size);
        PageResult<User> pageResult = new PageResult<>(pageList.getTotal(), pageList.getRecords());
        return Result.ok("查询成功", pageResult);
    }

    /**
     * 分页条件搜索实现用户表
     *
     * @param iUser
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList")
    public Result<PageResult<User>> pageList(@RequestBody User iUser, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<User> pageList = iUserService.pageList(iUser, page, size);
        PageResult<User> pageResult = new PageResult<>(pageList.getTotal(), pageList.getRecords());
        return Result.ok("查询成功", pageResult);
    }
    /**
     * 启动、
     * 停用
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status")
    public Result updateStatus(@RequestParam Long id,@RequestParam Integer status){
        iUserService.status(id,status);
        return new Result("修改成功");
    }
    /**
     * 重置密码
     */
    @PostMapping("/reset")
    public Result reset(@RequestBody List<Long> ids){
        iUserService.resetPwd(ids);
        return new Result("修改成功");
    }
    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        Integer login = iUserService.login(user.getUsername(), user.getPassword());
        if (login == -1) {
            return Result.fail("用户名或密码错误");
        } else if (login == -2) {
            return Result.fail("该用户不存在");
        } else if (login == -3) {
            return Result.fail("该用户已停用");
        }
        Map map = new HashMap();
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        map.put("role", user.getRole());
        String token = JwtUtils.encrypt(map, new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 7));

        return new Result("登录成功",token);
    }


}




